public class Leetcode {
}

//leetcode:79:单词搜索
class Solution1 {
    int m,n;
    boolean[][] visits;
    char[] word;
    int[] dx = {0,0,1,-1};
    int[] dy = {1,-1,0,0};
    public boolean exist(char[][] board, String _word) {
        m = board.length;
        n = board[0].length;
        visits = new boolean[m][n];
        word = _word.toCharArray();
        for(int i = 0; i < m; i++){
            for(int j = 0;j < n;j++){
                if(board[i][j] == word[0]){
                    visits[i][j] = true;
                    if(dfs(board,i,j,1)) return true;
                    visits[i][j] = false;
                }
            }
        }
        return false;
    }

    public boolean dfs(char[][] board,int i,int j,int pos){
        if(pos == word.length){
            return true;
        }
        for(int k = 0; k < 4;k++){
            int x = i+dx[k],y = j+dy[k];
            if(x >= 0 && x < m && y >= 0 && y < n && !visits[x][y] && board[x][y] == word[pos]){
                visits[x][y] = true;
                if(dfs(board,x,y,pos+1)) return true;
                visits[x][y] = false;
            }
        }
        return false;
    }
}

//leetcode:1219:黄金矿工
class Solution2 {
    int ret = 0;
    int m,n;
    int[] dx = {0,0,1,-1};
    int[] dy = {1,-1,0,0};
    boolean[][] visits;
    public int getMaximumGold(int[][] grid) {
        m = grid.length;
        n = grid[0].length;
        visits = new boolean[m][n];
        for(int i = 0; i < m;i++){
            for(int j = 0 ;j < n;j++){
                if(grid[i][j] != 0){
                    visits[i][j] = true;
                    dfs(grid,i,j,grid[i][j]);
                    visits[i][j] = false;
                }
            }
        }
        return ret;
    }

    public void dfs(int[][] grid,int i ,int j, int path){
        ret = Math.max(ret,path);
        for(int k = 0; k < 4;k++){
            int x = i+dx[k],y = j+dy[k];
            if(x >= 0 && x < m && y >=0 && y < n && !visits[x][y] && grid[x][y] != 0){
                visits[x][y] = true;
                dfs(grid,x,y,path+grid[x][y]);
                visits[x][y] = false;
            }
        }
    }
}

//leetcode:980:不同路径III
class Solution3 {
    int ret ,m,n,count;
    boolean[][] check;
    int[] dx = {0,0,1,-1};
    int[] dy = {1,-1,0,0};
    public int uniquePathsIII(int[][] grid) {
        m = grid.length;
        n = grid[0].length;
        check = new boolean[m][n];
        int bx = 0,by = 0;
        for(int i = 0; i < m;i++){
            for(int j = 0;j < n;j++){
                if(grid[i][j] == 0){
                    count++;
                }else if(grid[i][j] == 1){
                    bx = i;
                    by = j;
                }
            }
        }
        check[bx][by] = true;
        dfs(grid,bx ,by,1);
        return ret;
    }
    public void dfs(int[][] grid,int i,int j,int pos){
        if(grid[i][j] == 2){
            if(pos == count+2){
                ret++;
            }
            return;
        }

        for(int k = 0; k < 4;k++){
            int x = i+dx[k],y = j+dy[k];
            if(x >= 0 && x < m && y >= 0 && y < n && !check[x][y] && grid[x][y] != -1){
                check[x][y] = true;
                dfs(grid,x,y,pos+1);
                check[x][y] = false;
            }
        }
    }
}